﻿Public Class xtf_ABMconfig_interfaz

    Dim ulg_row As DataRow
    Dim obj_config_interfaz As cls_config_interfaz_ = Nothing
    Dim dtt_modulo_ As DataTable
    Dim dtt_config_parametro_ As DataTable


    Dim interfazActual As String = ""

    Public Sub New()
        InitializeComponent()
        AgregarValidaciones()
        btn_Registro.Visible = False

        dtt_modulo_ = New DataTable

        dtt_modulo_.Columns.Add("Codigo")
        dtt_modulo_.Columns.Add("Grupo")
        dtt_modulo_.Columns.Add("SubGrupo")
        dtt_modulo_.Columns.Add("Activo", GetType(Boolean))

        gdc_modulo_.DataSource = dtt_modulo_
        gdv_modulo_.PopulateColumns()
        gdv_modulo_.Columns("Codigo").Visible = False
        gdv_modulo_.Columns("Codigo").OptionsColumn.AllowEdit = False

        gdv_modulo_.Columns("Grupo").ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemTextEdit
        gdv_modulo_.Columns("SubGrupo").ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemTextEdit
        gdv_modulo_.Columns("Activo").ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit


        dtt_config_parametro_ = New DataTable
        dtt_config_parametro_.Columns.Add("Codigo")
        dtt_config_parametro_.Columns.Add("Tab")
        dtt_config_parametro_.Columns.Add("Grupo")
        dtt_config_parametro_.Columns.Add("Etiqueta")
        dtt_config_parametro_.Columns.Add("Activo", GetType(Boolean))

        gdc_parametro_.DataSource = dtt_config_parametro_
        gdv_parametro_.PopulateColumns()
        gdv_parametro_.Columns("Codigo").Visible = False
        gdv_parametro_.Columns("Tab").OptionsColumn.AllowEdit = False
        gdv_parametro_.Columns("Grupo").OptionsColumn.AllowEdit = False

        gdv_parametro_.Columns("Etiqueta").ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemTextEdit
        gdv_parametro_.Columns("Activo").ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit

        cargarModulos()
        cargarParametros()


        Dim obj_config_proveedor As New cls_config_proveedor
        obj_config_proveedor.Buscar()
        txt_proveedornombre.Text = obj_config_proveedor.nombre
        txt_proveedordescripcion.Text = obj_config_proveedor.descripcion
        txt_proveedortelefono.Text = obj_config_proveedor.telefono
        txt_proveedordireccion.Text = obj_config_proveedor.direccion
        txt_proveedorweb.Text = obj_config_proveedor.web
        txt_proveedoremail.Text = obj_config_proveedor.email
        txt_proveedorlogo.Text = obj_config_proveedor.logo

        Dim obj_config_producto As New cls_config_producto
        obj_config_producto.Buscar()
        txt_productoNombre.Text = obj_config_producto.nombre
        txt_productoDescripcion.Text = obj_config_producto.descripcion
        txt_productosplash.Text = obj_config_producto.splash
        txt_productologo.Text = obj_config_producto.logo


        SimpleButton1_Click(Nothing, Nothing)
        interfazActual = txt_script.Text
    End Sub

    Private Sub cargarModulos()
        dtt_modulo_.Rows.Clear()
        Dim config_modulo_ As New cls_config_modulo_
        config_modulo_.CargarTodos()
        For Each config_modulo As cls_config_modulo_ In config_modulo_.Items
            Try
                Dim row As DataRow = dtt_modulo_.NewRow
                row("Codigo") = config_modulo.idmodulo
                row("Grupo") = config_modulo.modulo.grupo
                row("SubGrupo") = config_modulo.modulo.subgrupo
                row("Activo") = config_modulo.activo
                dtt_modulo_.Rows.Add(row)
            Catch ex As Exception
            End Try
        Next
    End Sub

    Private Sub cargarParametros()
        dtt_config_parametro_.Rows.Clear()
        Dim config_config_parametro_ As New cls_config_parametro_
        config_config_parametro_.CargarTodos()
        For Each config_config_parametro As cls_config_parametro_ In config_config_parametro_.Items
            Dim row As DataRow = dtt_config_parametro_.NewRow
            row("Codigo") = config_config_parametro.idparametro
            row("Tab") = config_config_parametro.tab
            row("Grupo") = config_config_parametro.grupo
            row("Etiqueta") = config_config_parametro.label
            row("Activo") = config_config_parametro.visible
            dtt_config_parametro_.Rows.Add(row)
        Next
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            obj_config_interfaz = New cls_config_interfaz_
            btn_Registro.Enabled = False

        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef config_interfaz As cls_config_interfaz_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_config_interfaz) Then
            obj_config_interfaz = New cls_config_interfaz_
        End If
        obj_config_interfaz = config_interfaz
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_config_interfaz.ToString
        cambiarEntorno(obj_config_interfaz.script)

        txt_id.Text = obj_config_interfaz.idconfig_interfaz
        txt_Nombre.Text = obj_config_interfaz.nombre
        txt_script.Text = obj_config_interfaz.script

        cargarModulos()
        cargarParametros()

        Dim obj_config_proveedor As New cls_config_proveedor
        obj_config_proveedor.Buscar()
        txt_proveedornombre.Text = obj_config_proveedor.nombre
        txt_proveedordescripcion.Text = obj_config_proveedor.descripcion
        txt_proveedortelefono.Text = obj_config_proveedor.telefono
        txt_proveedordireccion.Text = obj_config_proveedor.direccion
        txt_proveedorweb.Text = obj_config_proveedor.web
        txt_proveedoremail.Text = obj_config_proveedor.email
        txt_proveedorlogo.Text = obj_config_proveedor.logo

        Dim obj_config_producto As New cls_config_producto
        obj_config_producto.Buscar()
        txt_productoNombre.Text = obj_config_producto.nombre
        txt_productoDescripcion.Text = obj_config_producto.descripcion
        txt_productosplash.Text = obj_config_producto.splash
        txt_productologo.Text = obj_config_producto.logo


    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not chk_manual.Checked Then
            SimpleButton1_Click(Nothing, Nothing)
        End If
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_config_interfaz.nombre = txt_Nombre.Text
            obj_config_interfaz.script = txt_script.Text
            If txt_id.Text = "" Then
                obj_config_interfaz.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_LISTADOconfig_interfaz_).AgregarItem(obj_config_interfaz)
                End If
                Modificar(obj_config_interfaz, frm_listado, ulg_row)
            Else
                obj_config_interfaz.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_LISTADOconfig_interfaz_).ModificarItem(obj_config_interfaz, ulg_row)
                End If
                Modificar(obj_config_interfaz, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If

        'Ejecucion de script de cambio de entorno
        Dim objParametroEntorno As New cls_config_parametro_
        objParametroEntorno.Buscar("interfaz")
        If txt_id.Text = objParametroEntorno.valor Then
            If interfazActual <> obj_config_interfaz.script Then
                'cambiarEntorno(interfazActual)
                bol_comprobarCerrado = False
                If ErrManagerShow("001036", Me) = Windows.Forms.DialogResult.Yes Then
                    cambiarEntorno(obj_config_interfaz.script)
                    If Not IsNothing(CType(Application.OpenForms("xtf_Main"), xtf_Main)) Then
                        CType(Application.OpenForms("xtf_Main"), xtf_Main).IniciarUsuario()
                    End If
                Else
                    cambiarEntorno(interfazActual)
                End If
            Else
                cambiarEntorno(interfazActual)
            End If
        Else
            cambiarEntorno(interfazActual)
        End If

        Me.Dispose()
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim SQL As String = ""

        'CONFIG PRODUCTO-----------------------------------
        SQL &= "--CONFIGURACION DEL PRODUCTO" & vbNewLine
        SQL &= "update config_producto set" & vbNewLine
        SQL &= "nombre = '" & txt_productoNombre.Text & "'," & vbNewLine
        SQL &= "Descripcion = '" & txt_productoDescripcion.Text & "'," & vbNewLine
        SQL &= "Splash = '" & txt_productosplash.Text & "'," & vbNewLine
        SQL &= "logo = '" & txt_productologo.Text & "'" & vbNewLine
        SQL &= "GO" & vbNewLine
        SQL &= "-----------------------------" & vbNewLine
        SQL &= vbNewLine

        'CONFIG PROVEEDOR----------------------------------
        SQL &= "--CONFIGURACION DEL PROVEEDOR" & vbNewLine
        SQL &= "update config_proveedor set" & vbNewLine
        SQL &= "logo = '" & txt_proveedorlogo.Text & "'," & vbNewLine
        SQL &= "nombre = '" & txt_proveedornombre.Text & "'," & vbNewLine
        SQL &= "descripcion = '" & txt_proveedordescripcion.Text & "'," & vbNewLine
        SQL &= "telefono = '" & txt_proveedortelefono.Text & "'," & vbNewLine
        SQL &= "direccion = '" & txt_proveedordireccion.Text & "'," & vbNewLine
        SQL &= "email = '" & txt_proveedoremail.Text & "'," & vbNewLine
        SQL &= "web = '" & txt_proveedorweb.Text & "'" & vbNewLine
        SQL &= "GO" & vbNewLine
        SQL &= "----------------------------" & vbNewLine
        SQL &= vbNewLine

        'SE ACTIVAN TODOS LOS MODULOS-----------------------
        SQL &= "--ACTIVACION DE MODULOS-----" & vbNewLine
        SQL &= "update config_modulo_ set" & vbNewLine
        SQL &= "activo = 1                 --SE ACTIVAN TODOS LOS MODULOS" & vbNewLine
        SQL &= "GO" & vbNewLine
        SQL &= "----------------------------" & vbNewLine
        SQL &= vbNewLine

        'SE ACTIVAN TODOS LOS PARAMETROS--------------------
        SQL &= "--ACTIVACION DE PARAMETROS--" & vbNewLine
        SQL &= "update config_parametro_ set" & vbNewLine
        SQL &= "parametro__visible = 1" & vbNewLine
        SQL &= "GO" & vbNewLine
        SQL &= "----------------------------" & vbNewLine
        SQL &= vbNewLine

        'DESACTIVACION DE MODULOS--------------------------
        SQL &= "--DESACTIVACION DE MODULOS--" & vbNewLine
        SQL &= "update config_modulo_ set" & vbNewLine
        SQL &= "activo = 0" & vbNewLine
        SQL &= "WHERE" & vbNewLine
        SQL &= "idmodulo = '' OR" & vbNewLine
        For Each row As DataRow In dtt_modulo_.Rows
            If row("Activo") = False Then
                SQL &= "idmodulo = '" & row("Codigo") & "' OR " & vbNewLine
            End If
        Next
        SQL = Strings.Left(SQL, SQL.Length - 5) & vbNewLine
        SQL &= "GO" & vbNewLine
        SQL &= "----------------------------" & vbNewLine
        SQL &= vbNewLine

        'DESACTIVACION DE PARAMETROS-----------------------
        SQL &= "--DESACTIVACION DE PARAMETROS" & vbNewLine
        SQL &= "update config_parametro_ set" & vbNewLine
        SQL &= "parametro__visible = 0" & vbNewLine
        SQL &= "WHERE" & vbNewLine
        SQL &= "parametro__idparametro = '' OR" & vbNewLine
        For Each row As DataRow In dtt_config_parametro_.Rows
            If row("Activo") = False Then
                SQL &= "parametro__idparametro = '" & row("Codigo") & "' OR " & vbNewLine
            End If
        Next
        SQL = Strings.Left(SQL, SQL.Length - 5) & vbNewLine
        SQL &= "GO" & vbNewLine
        SQL &= "----------------------------" & vbNewLine
        SQL &= vbNewLine

        'CAMBIO DE NOMBRE MODULOS-----------------------
        SQL &= "--CAMBIO DE NOMBRE MODULOS" & vbNewLine
        For Each row As DataRow In dtt_modulo_.Rows
            SQL &= "update modulo_ set" & vbNewLine
            SQL &= "grupo = '" & row("Grupo") & "'," & vbNewLine
            SQL &= "subgrupo = '" & row("SubGrupo") & "'" & vbNewLine
            SQL &= "WHERE" & vbNewLine
            SQL &= "(idmodulo LIKE '" & Strings.Left(row("Codigo"), row("Codigo").Length - 3) & "%')" & vbNewLine
            SQL &= "GO" & vbNewLine
        Next

        'CAMBIO DE NOMBRE PARAMETROS-----------------------
        SQL &= "--CAMBIO DE NOMBRE PARAMETROS" & vbNewLine
        For Each row As DataRow In dtt_config_parametro_.Rows
            SQL &= "update config_parametro_ set" & vbNewLine
            SQL &= "parametro__label = '" & row("Etiqueta") & "'" & vbNewLine
            SQL &= "WHERE" & vbNewLine
            SQL &= "parametro__idparametro = '" & row("Codigo") & "'" & vbNewLine
            SQL &= "GO" & vbNewLine
        Next

        SQL &= vbNewLine
        SQL &= "--FIN SCRIPT--" & vbNewLine

        txt_script.Text = SQL
    End Sub


    Private Sub chk_manual_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_manual.CheckedChanged
        txt_script.Properties.ReadOnly = Not chk_manual.Checked
        btn_seleccionarTodo.Enabled = chk_manual.Checked
        btn_copiar.Enabled = chk_manual.Checked
    End Sub

    Private Sub btn_seleccionarTodo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_seleccionarTodo.Click
        txt_script.Focus()
        txt_script.SelectAll()
    End Sub

    Private Sub btn_copiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_copiar.Click
        My.Computer.Clipboard.SetText(txt_script.Text)
    End Sub

    Private Sub btn_guardarScript_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_guardarScript.Click
        Dim SFD As New SaveFileDialog
        SFD.Filter = "Archivos de texto plano (*.txt)|*.txt|Todos los Archivos (*.*)|*.*"
        If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Using str_Writer As IO.StreamWriter = New IO.StreamWriter(SFD.FileName, False, System.Text.Encoding.Unicode)
                str_Writer.WriteLine(txt_script.Text)
                str_Writer.Close()
            End Using
        End If
    End Sub
End Class


'Dim SFD As New SaveFileDialog
'        SFD.Filter = "INI Files (*.INI)|*.INI|All Files (*.*)|*.*"
'        If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
'            Using str_Writer As IO.StreamWriter = New IO.StreamWriter(SFD.FileName, False, System.Text.Encoding.Unicode)
'                str_Writer.WriteLine(txt_resultado.Text)
'                str_Writer.Close()
'            End Using
'        End If